package com.dmooo.ymx.mall;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dmooo.ymx.R;
import com.dmooo.ymx.adapter.MyOderViewPagerAdapter;
import com.dmooo.ymx.base.BaseLazyFragment;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.mallbean.MallCatbean;
import com.dmooo.ymx.widget.indicator.MagicIndicator;
import com.dmooo.ymx.widget.indicator.ViewPagerHelper;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.CommonNavigator;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.abs.IPagerIndicator;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.abs.IPagerTitleView;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import com.dmooo.ymx.widget.indicator.buildins.commonnavigator.titles.ClipPagerTitleView;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class ShopMallHomeFragment extends BaseLazyFragment {


    @BindView(R.id.tabBar)
    MagicIndicator tabBar;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private int index;
    View view;
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mall_home, container, false);
         ButterKnife.bind(this, view);
        initData();
        return view;
    }

    protected void initData() {
        view.findViewById(R.id.img_shop_car).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(MallGoodsCartActivity.class);
            }
        });
        view.findViewById(R.id.img_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ShopMallActivity.class);
            }
        });
        view.findViewById(R.id.shop_my).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ShopMallMineActivity.class);
            }
        });
//        ShopMallFragment homeTabsFragment = new ShopMallFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("pid", getIntent().getExtras().getString("pid"));
//        bundle.putString("name", getIntent().getExtras().getString("name"));
//        homeTabsFragment.setArguments(bundle);
//        fragments.add(homeTabsFragment);
//        viewpager.setOffscreenPageLimit(fragments.size());
//        //适配器
//        MyOderViewPagerAdapter myOderViewPagerAdapter = new MyOderViewPagerAdapter(getSupportFragmentManager(), fragments);
//        viewpager.setAdapter(myOderViewPagerAdapter);
//        //头部导航栏
//        CommonNavigator commonNavigator = new CommonNavigator(getComeActivity());
//        commonNavigator.setSkimOver(true);
//        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
//
//            @Override
//            public int getCount() {
//                return 1;
//            }
//
//            @Override
//            public IPagerTitleView getTitleView(Context context, final int index) {
//                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
//                clipPagerTitleView.setText("");
//                clipPagerTitleView.setTextColor(getResources().getColor(R.color.col_999));
//                clipPagerTitleView.setClipColor(getResources().getColor(R.color.red1));
//                clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        viewpager.setCurrentItem(index);
//                    }
//                });
//                return clipPagerTitleView;
//            }
//
//            @Override
//            public IPagerIndicator getIndicator(Context context) {
//                LinePagerIndicator indicator = new LinePagerIndicator(context);
//                indicator.setMode(1);
//                indicator.setColors(getResources().getColor(R.color.red1));
//                return indicator;
//            }
//        });
//        tabBar.setNavigator(commonNavigator);
//        ViewPagerHelper.bind(tabBar, viewpager);
//        //设置页数
//        viewpager.setCurrentItem(index);
        getTopCatListRequst();
    }
    /**
     * @属性:获取顶级淘宝商品分类列表
     * @开发者:陈飞
     * @时间:2018/7/26 15:31
     */
    private void getTopCatListRequst() {
        RequestParams requestParams = new RequestParams();
        HttpUtils.post(Constants.SLEF_MALL_CAT, requestParams, new onOKJsonHttpResponseHandler<MallCatbean>(new TypeToken<Response<MallCatbean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Response<MallCatbean> datas) {
                if (datas.isSuccess()) {
                    final List<MallCatbean> list = datas.getData().list;
                    MallCatbean shopTabsChildBean = new MallCatbean();
                    shopTabsChildBean.cat_id="";
                    shopTabsChildBean.cat_name="精选";
                    list.add(0,shopTabsChildBean);
                    fragments.clear();
                    for (int i = 0; i < list.size(); i++) {
                        shopTabsChildBean = list.get(i);
                        Fragment homeTabsFragment;
                        if(i==0){
                            homeTabsFragment = new MallFragmentFirst();
                        }else{
                            homeTabsFragment = new ShopMallFragment();
                        }
                        Bundle bundle = new Bundle();
                        bundle.putString("pid", shopTabsChildBean.cat_id);
                        bundle.putString("name", shopTabsChildBean.cat_name);
                        homeTabsFragment.setArguments(bundle);
                        fragments.add(homeTabsFragment);
                    }
                    viewpager.setOffscreenPageLimit(fragments.size());
                    //适配器
                    MyOderViewPagerAdapter myOderViewPagerAdapter = new MyOderViewPagerAdapter(getChildFragmentManager(), fragments);
                    viewpager.setAdapter(myOderViewPagerAdapter);
                    //头部导航栏
                    CommonNavigator commonNavigator = new CommonNavigator(getActivity());
                    commonNavigator.setSkimOver(true);
                    commonNavigator.setAdapter(new CommonNavigatorAdapter() {

                        @Override
                        public int getCount() {
                            return list.size();
                        }

                        @Override
                        public IPagerTitleView getTitleView(Context context, final int index) {
                            ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
                            clipPagerTitleView.setText(list.get(index).cat_name);
                            clipPagerTitleView.setTextColor(getResources().getColor(R.color.col_999));
                            clipPagerTitleView.setClipColor(getResources().getColor(R.color.red1));
                            clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    viewpager.setCurrentItem(index);
                                }
                            });
                            return clipPagerTitleView;
                        }

                        @Override
                        public IPagerIndicator getIndicator(Context context) {
                            LinePagerIndicator indicator = new LinePagerIndicator(context);
                            indicator.setMode(1);
                            indicator.setColors(getResources().getColor(R.color.red1));
                            return indicator;
                        }
                    });
                    tabBar.setNavigator(commonNavigator);
                    ViewPagerHelper.bind(tabBar, viewpager);
                    //设置页数
                    viewpager.setCurrentItem(index);
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    @Override
    protected void lazyload() {

    }

}
